



var obj_set;// 画布中操作的形状实例数组
var draw;
var cmdstack = new CommandStack();
// var goDrawpanel;
var canvas_offset_top, canvas_offset_left;
var shift_key_on = false;


$(function() {
	
	$.ajaxSetup({
		timeout: 3000,
		dataType: 'html',
		//请求成功后触发
		success: function (data) {},
		//请求失败遇到异常触发
		error: function (xhr, status, e) {},
		//完成请求后触发。即在success或error触发后触发
		complete: function (xhr, status) {},
		//发送请求前触发
		beforeSend: function (xhr) {
			//判断 是否超时
		
		}
	})
	  
	if ($.cookie("lg") != undefined && $.cookie("lg") == "1") {
		$('#loginzone').modal('hide')
		$('#userlable').html($.cookie("userId"));
		$('#loginPageHref').html('注销');
	}
	canvas_offset_top = $("#canvas").offset().top;
	canvas_offset_left = $("#canvas").offset().left;
	var _height = $(window).height() - canvas_offset_top;// 获取当前窗口的高度
	$('#canvas').css('height', _height * 0.92);// 调整列表的宽高
	
	draw = SVG('canvas');
	obj_set = draw.set();// 初始化数组
	all_selected_ele_set = draw.set();// 初始化数组
	$("#loginPageHref").click(login_nag);//登录导航
	$("#editorsavebtn").click(editorSaveAct);//属性编辑器导航
	$("#editorPanel").draggable();//设置属性编辑器可拖动
	$("#mypainthref").click(mypaintNav);//我的绘图导航
	$("#myiconhref").click(myiconNav);//图案导航
	$("#cooperationhref").click(cooperationNav);//协作导航
	$("#drawhref").click(drawNav);//画布导航
	$("#saveBtn").click(canvas_save);//画布保存按钮

	// 颜色板
	$(".edit-color-span").click(open_color_plate);
	// 登录面板
	$('#loginpanel a').click(function(event) {
		event.preventDefault()
		$(this).tab('show');
	});
	// 图标按钮面板
	$('#imgtoolpanel a').click(tools_callback)
	//注册事件
	$("#model_register_btn").click(register);
	//登录事件
	$("#model_login_btn").click(login);
	//形状选择右侧的编辑工具栏
	$("#edittoobtn").click(dblclick);
	$('#imagezone').on('hidden.bs.modal', function (e) {
		var $container = $('#iconsfluidDiv');
		$container.empty();
		readedpage=1;
	})
	
	document.body.addEventListener('touchmove', function (event) {
		event.preventDefault();
	}, false);
	
	/**画布事件*/
	var dom = document.getElementById("canvas");
	SVG.on(dom, 'mousedown', mouseclick);
	SVG.on(dom, 'touchstart.', mouseclick);
	SVG.on(dom, 'mousemove', mousemove);
	SVG.on(dom, 'touchmove.', mousemove);
	SVG.on(dom, 'mouseup.', mouseup);
	SVG.on(dom, 'touchend.', mouseup);
	SVG.on(window, 'copy', oncopy);
	SVG.on(window, 'keydown', keydown);
	//SVG.on(dom, 'dblclick.', dblclick);
	SVG.on(window, 'paste', onpaste)

	droppable_api();

});
function draw_img(_imgsrc){
	
	$("#imagezone").modal("hide");
	// var image = draw.image(_imgsrc,100,100).transform({
	// scaleX:1,scaleY:1 });
	var image = draw.image(_imgsrc, 100, 100).loaded(function(loader) {
		this.size(loader.width, loader.height)
	}).transform({
		scaleX : 1,
		scaleY : 1
	});
	/**TODO:注释掉放置后的位置***/
	//image.move(event.clientX, event.clientY - canvas_offset_top);
	image.move(400, 200 - canvas_offset_top);
	// image.resize();
	// image.selectize();
	obj_set.add(image);
	cmdstack.undostack.push(image);
}
function droppable_api(){
	/**从图标工具集中拖拽图标到画布
	 * 注意:拖拽图标需加上class="draw2d_droppable" */
	$("#canvas").droppable({
		accept : '.draw2d_droppable',
		over : function(event, ui) {

		},
		out : function(event, ui) {

		},
		drop : function(event, ui) {
			
			var _imgsrc = ui.helper.context.currentSrc;
			
			draw_img(_imgsrc)
		}
	});
}
/**
 * 工具栏功能按钮点击时触发的函数,依赖selected_tool_id全局变量
 */
function tool_select(ele) {
	selected_tool_id = ele.id;
	
	$("#toolbar>div").each(
		function(index,element){
			$(this).css("background-color","transparent");
		}
		
	);
	$(ele).css("background-color","#CCFFF");
	// var _node=$(ele);
	// var type = _node.data("shape");
	// var attr = _node.data("attr");
	if (selected_tool_id == "select") {// 选择模式
		// app.onDrawing=false;
		// app.paperdom.css({"cursor":"default"});
		isDrawing = false;
		$("#canvas").css({
			"cursor" : "default"
		});

	} else if (selected_tool_id == "delBbn") {// 删除
		isDrawing = false;
		selected_tool.selectize(false);
		selected_tool.resize(false);
		selected_tool.draggable(false);
		all_selected_ele_set.remove(selected_tool);
		selected_tool.remove();// 删除svg元素
		obj_set.remove(selected_tool);
		SVG.off(selected_tool.node, 'dblclick.')
		selected_tool=undefined;
		

	} else if (selected_tool_id == "undoBtn") {// 撤消
		isDrawing = false;
		cmdstack.undo();
	} else if (selected_tool_id == "redoBtn") {// 重做
		cmdstack.redo();
	} else if (selected_tool_id == "toback") {// 向后
		isDrawing = false;
		selected_tool.backward();
	} else if (selected_tool_id == "tofront") {// 向前
		isDrawing = false;
		selected_tool.forward();
	} else if (selected_tool_id == "image") {// 图片按钮
		isDrawing = false;
		$("#myiconsethref").trigger("click");
		
	} else {// 绘图模式
		selected_tool = undefined;// 清空当前实例
		isDrawing = true;

		$("#canvas").css({
			"cursor" : "crosshair"
		});
	}

	return;
}
